package net.zoneland.knowledge.dao;

import net.zoneland.knowledge.enums.AttachTypeEnum;
import net.zoneland.knowledge.enums.WorkStatusEnum;
import net.zoneland.knowledge.model.*;
import net.zoneland.knowledge.service.bean.WorkApplyQueryVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 智办工单数据接口.
 *
 * @author zmq
 * @Date 2022/8/9 13:32
 */
@Mapper
@MappedTypes({
        WorkStatusEnum.class,
        AttachTypeEnum.class
})
public interface WorkApplyInfoMapper {
    /**
     * 插入主工单信息对象.
     *
     * @param info 主工单信息
     */
    @Insert({
            "insert into AITK_WORK_APPLY_INFO (ID, PIID, ",
            "APPLY_ID, TITLE, ASSIGN_USER_ID, ASSIGN_USER_NAME, STATUS, DISPATCHED, ",
            "HANDLER_UID, HANDLER_NAME, ",
            "RECEIVER_UID, RECEIVER_NAME, ",
            "CREATOR_MOBILE, CREATOR_DEPT_DN, ",
            "CREATOR_DEPT_NAME, CREATOR_UID, PARENT_ID, ",
            "CREATOR_NAME, MAIN_TASK_NAME, CREATE_TIME, UPDATE_TIME, HANDLE_RESULT, ",
            "FLOW_TYPE, CREATOR_PARTY_ORG_NAME, CHAPTER_DATE,CHAPTER_DOC, ",
            "CHAPTER_DOC_OTHER, DOC_NUM,SEND_UNIT, CONTENT_SUMMARY,PARTY_LEADER_UID, ",
            "PARTY_LEADER_NAME, MAIN_WORK_APPLY_ID, NOTIFY_AFTER_REPORT_ALL, USESEALTYPE, ",
            "PROJECT_CATEGORY, KEY_AREAS_TO_TACKLE, DESCRIPTION, CONTACTS_UID, CONTACTS_NAME, ",
            "CONTACTS_MOBILE, PARTICIPANTS_UID, PARTICIPANTS_NAME, RECEIVER_LABEL, PARTICIPANTS_LABEL, CONTACTS_LABEL",
             ")",
            "values (#{id,jdbcType=VARCHAR}, #{piid,jdbcType=VARCHAR}, ",
            "#{applyId,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, ",
            "#{assignUserId,jdbcType=VARCHAR}, #{assignUserName,jdbcType=VARCHAR}, ",
            "#{status,jdbcType=VARCHAR}, #{dispatched,jdbcType=VARCHAR}, ",
            "#{handlerUid,jdbcType=VARCHAR}, #{handlerName,jdbcType=VARCHAR}, ",
            "#{receiverUid,jdbcType=VARCHAR}, #{receiverName,jdbcType=VARCHAR}, ",
            "#{creatorMobile,jdbcType=VARCHAR}, #{creatorDeptDn,jdbcType=VARCHAR}, ",
            "#{creatorDeptName,jdbcType=VARCHAR}, #{creatorUid,jdbcType=VARCHAR}, #{parentId,jdbcType=VARCHAR}, ",
            "#{creatorName,jdbcType=VARCHAR}, #{mainTaskName,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, ",
            "#{updateTime,jdbcType=TIMESTAMP}, #{handleResult,jdbcType=TIMESTAMP},",
            "#{flowType,jdbcType=VARCHAR}, #{creatorPartyOrgName,jdbcType=VARCHAR},",
            "#{chapterDate,jdbcType=TIMESTAMP}, #{chapterDoc,jdbcType=VARCHAR},#{chapterDocOther,jdbcType=VARCHAR},",
            "#{docNum,jdbcType=INTEGER}, #{sendUnit,jdbcType=VARCHAR},",
            "#{contentSummary,jdbcType=VARCHAR}, #{partyLeaderUid,jdbcType=VARCHAR},",
            "#{partyLeaderName,jdbcType=VARCHAR}, #{mainWorkApplyId,jdbcType=VARCHAR}, #{notifyAfterReportAll,jdbcType=BOOLEAN}, ",
            "#{useSealType,jdbcType=VARCHAR}, #{projectCategory,jdbcType=VARCHAR}, #{keyAreasToTackle,jdbcType=VARCHAR}, ",
            "#{description,jdbcType=VARCHAR}, #{contactsUid,jdbcType=VARCHAR}, #{contactsName,jdbcType=VARCHAR}, ",
            "#{contactsMobile,jdbcType=VARCHAR}, #{participantsUid,jdbcType=VARCHAR}, #{participantsName,jdbcType=VARCHAR}, ",
            "#{receiverLabel,jdbcType=VARCHAR}, #{participantsLabel,jdbcType=VARCHAR}, #{contactsLabel,jdbcType=VARCHAR}",
            ")"
    })
    void insertWorkApplyInfo(final WorkApplyInfo info);

    /**
     * 通过id查询主工单信息对象.
     *
     * @param id 主工单id
     * @return 主工单
     */
    @Select({
            "select ID, PIID, ",
            "APPLY_ID, TITLE, ASSIGN_USER_ID, ASSIGN_USER_NAME, STATUS, ",
            "HANDLER_UID, HANDLER_NAME, HANDLE_RESULT, ",
            "RECEIVER_UID, RECEIVER_NAME, ",
            "CREATOR_MOBILE, CREATOR_DEPT_DN, DISPATCHED, ",
            "CREATOR_DEPT_NAME, CREATOR_UID, PARENT_ID, ",
            "CREATOR_NAME, MAIN_TASK_NAME, CREATE_TIME, UPDATE_TIME, ",
            "FLOW_TYPE,CREATOR_PARTY_ORG_NAME,CHAPTER_DATE,CHAPTER_DOC,",
            "CHAPTER_DOC_OTHER,DOC_NUM,SEND_UNIT,CONTENT_SUMMARY,PARTY_LEADER_UID,",
            "PARTY_LEADER_NAME, MAIN_WORK_APPLY_ID, ",
            "CURRENT_STATUS, CURRENT_HANDLER_UID, CURRENT_HANDLER_NAME, NOTIFY_AFTER_REPORT_ALL, USESEALTYPE, ",
            "KEY_AREAS_TO_TACKLE, PROJECT_CATEGORY, CONTACTS_UID, CONTACTS_NAME, CONTACTS_MOBILE, ",
            "PARTICIPANTS_UID, PARTICIPANTS_NAME, DESCRIPTION, RECEIVER_LABEL, PARTICIPANTS_LABEL, CONTACTS_LABEL ",
            "FROM aitk_work_apply_view ",
            "WHERE ID=#{id,jdbcType=VARCHAR}"
    })
    @Results(id = "WorkApplyInfoResultMap", value = {
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "PIID", property = "piid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "APPLY_ID", property = "applyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "TITLE", property = "title", jdbcType = JdbcType.VARCHAR),
            @Result(column = "STATUS", property = "status", jdbcType = JdbcType.VARCHAR),
            @Result(column = "HANDLER_UID", property = "handlerUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "HANDLER_NAME", property = "handlerName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "HANDLE_RESULT", property = "handleResult", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ASSIGN_USER_ID", property = "assignUserId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ASSIGN_USER_NAME", property = "assignUserName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENT_ID", property = "parentId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "RECEIVER_UID", property = "receiverUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "RECEIVER_NAME", property = "receiverName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATOR_MOBILE", property = "creatorMobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATOR_DEPT_DN", property = "creatorDeptDn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DISPATCHED", property = "dispatched", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "CREATOR_DEPT_NAME", property = "creatorDeptName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATOR_UID", property = "creatorUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATOR_NAME", property = "creatorName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MAIN_TASK_NAME", property = "mainTaskName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATE_TIME", property = "createTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "FLOW_TYPE", property = "flowType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATOR_PARTY_ORG_NAME", property = "creatorPartyOrgName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CHAPTER_DATE", property = "chapterDate", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "CHAPTER_DOC", property = "chapterDoc", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CHAPTER_DOC_OTHER", property = "chapterDocOther", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DOC_NUM", property = "docNum", jdbcType = JdbcType.INTEGER),
            @Result(column = "SEND_UNIT", property = "sendUnit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CONTENT_SUMMARY", property = "contentSummary", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARTY_LEADER_UID", property = "partyLeaderUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARTY_LEADER_NAME", property = "partyLeaderName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "MAIN_WORK_APPLY_ID", property = "mainWorkApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CURRENT_STATUS", property = "currentStatus", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CURRENT_HANDLER_UID", property = "currentHandlerUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CURRENT_HANDLER_NAME", property = "currentHandlerName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NOTIFY_AFTER_REPORT_ALL", property = "notifyAfterReportAll", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "USESEALTYPE", property = "useSealType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "KEY_AREAS_TO_TACKLE", property = "keyAreasToTackle", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PROJECT_CATEGORY", property = "projectCategory", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CONTACTS_UID", property = "contactsUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CONTACTS_NAME", property = "contactsName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CONTACTS_MOBILE", property = "contactsMobile", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARTICIPANTS_UID", property = "participantsUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARTICIPANTS_NAME", property = "participantsName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DESCRIPTION", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "RECEIVER_LABEL", property = "receiverLabel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARTICIPANTS_LABEL", property = "participantsLabel", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CONTACTS_LABEL", property = "contactsLabel", jdbcType = JdbcType.VARCHAR),
    })
    WorkApplyInfo findWorkApplyInfoById(final String id);

    /**
     * 通过父工单id查询所有子工单信息对象.
     *
     * @param parentApplyId 父工单id
     * @return 主工单
     */
    @Select({
            "select ID, PIID, ",
            "APPLY_ID, TITLE, ASSIGN_USER_ID, ASSIGN_USER_NAME, STATUS, ",
            "HANDLER_UID, HANDLER_NAME, HANDLE_RESULT, ",
            "RECEIVER_UID, RECEIVER_NAME, ",
            "CREATOR_MOBILE, CREATOR_DEPT_DN, DISPATCHED, ",
            "CREATOR_DEPT_NAME, CREATOR_UID, PARENT_ID, ",
            "CREATOR_NAME, MAIN_TASK_NAME, CREATE_TIME, UPDATE_TIME, ",
            "FLOW_TYPE,CREATOR_PARTY_ORG_NAME,CHAPTER_DATE,CHAPTER_DOC,",
            "CHAPTER_DOC_OTHER,DOC_NUM,SEND_UNIT,CONTENT_SUMMARY,PARTY_LEADER_UID,",
            "PARTY_LEADER_NAME, MAIN_WORK_APPLY_ID,NOTIFY_AFTER_REPORT_ALL ",
            "FROM AITK_WORK_APPLY_INFO ",
            "WHERE PARENT_ID=#{parentApplyId,jdbcType=VARCHAR}"
    })
    @ResultMap(value = {"WorkApplyInfoResultMap"})
    List<WorkApplyInfo> findWorkApplyInfoByParentApplyId(final String parentApplyId);

    /**
     * 插入分任务信息.
     *
     * @param info 分任务信息
     */
    @Insert({
            "insert into AITK_WORK_ST_INFO (ID, NAME, ",
            "DESCRIPTION, DEPT_DNS, ",
            "DEPT_NAMES, FINISH_TIME, ",
            "WORK_APPLY_ID, PARENT_WORK_APPLY_ID,  ",
            "PARENT_TASK_ID, NODE, STATE, UPDATE_TIME, ORDER_INDEX,",
            "MAIN_TASK_ID, REPORT_MERGE_ATTACHMENT, REPORT_COUNT, AUTO_MERGE_ATTACHMENT,REMOVE_HANDLER,APPLY_SHARED_EDITING)",
            "values ",
            "(#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, ",
            "#{description,jdbcType=VARCHAR}, #{deptDns,jdbcType=VARCHAR}, ",
            "#{deptNames,jdbcType=VARCHAR}, #{finishTime,jdbcType=TIMESTAMP}, ",
            "#{workApplyId,jdbcType=VARCHAR}, #{parentWorkApplyId,jdbcType=VARCHAR}, ",
            "#{parentTaskId,jdbcType=VARCHAR}, #{node,jdbcType=VARCHAR}, ",
            "#{state,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, #{orderIndex,jdbcType=INTEGER},",
            "#{mainTaskId,jdbcType=VARCHAR}, #{reportMergeAttachment,jdbcType=BOOLEAN}, #{reportCount,jdbcType=INTEGER},",
            "#{autoMergeAttachment,jdbcType=BOOLEAN}, #{removeHandler,jdbcType=BOOLEAN}, #{applySharedEditing,jdbcType=BOOLEAN})"
    })
    void insertSubTask(WorkSubTaskInfo info);

    /**
     * 通过主工单id查询分任务信息对象.
     *
     * @param workApplyId 主工单id
     * @return 分任务列表
     */
    @Select({
            "select ID, NAME, ",
            "DESCRIPTION, DEPT_DNS, ",
            "DEPT_NAMES, FINISH_TIME, ",
            "WORK_APPLY_ID, PARENT_WORK_APPLY_ID, ",
            "PARENT_TASK_ID, NODE, STATE, UPDATE_TIME, ORDER_INDEX,",
            "MAIN_TASK_ID, REPORT_MERGE_ATTACHMENT,REPORT_COUNT,AUTO_MERGE_ATTACHMENT,REMOVE_HANDLER,APPLY_SHARED_EDITING ",
            "FROM AITK_WORK_ST_INFO ",
            "WHERE WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} order by ORDER_INDEX"
    })
    @Results(id = "WorkSubTaskInfoResultMap", value = {
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "NAME", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DESCRIPTION", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DEPT_DNS", property = "deptDns", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DEPT_NAMES", property = "deptNames", jdbcType = JdbcType.VARCHAR),
            @Result(column = "FINISH_TIME", property = "finishTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENT_WORK_APPLY_ID", property = "parentWorkApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "PARENT_TASK_ID", property = "parentTaskId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NODE", property = "node", jdbcType = JdbcType.VARCHAR),
            @Result(column = "STATE", property = "state", jdbcType = JdbcType.VARCHAR),
            @Result(column = "UPDATE_TIME", property = "updateTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "ORDER_INDEX", property = "orderIndex", jdbcType = JdbcType.INTEGER),
            @Result(column = "MAIN_TASK_ID", property = "mainTaskId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "REPORT_MERGE_ATTACHMENT", property = "reportMergeAttachment", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "REPORT_COUNT", property = "reportCount", jdbcType = JdbcType.INTEGER),
            @Result(column = "AUTO_MERGE_ATTACHMENT", property = "autoMergeAttachment", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "REMOVE_HANDLER", property = "removeHandler", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "APPLY_SHARED_EDITING", property = "applySharedEditing", jdbcType = JdbcType.BOOLEAN)
    })
    List<WorkSubTaskInfo> findWorkSubTasksByApplyId(final String workApplyId);

    @Select({
            "select ID, NAME, ",
            "DESCRIPTION, DEPT_DNS, ",
            "DEPT_NAMES, FINISH_TIME, ",
            "WORK_APPLY_ID, PARENT_WORK_APPLY_ID, ",
            "PARENT_TASK_ID, NODE, STATE, UPDATE_TIME, ORDER_INDEX,",
            "MAIN_TASK_ID, REPORT_MERGE_ATTACHMENT, REPORT_COUNT, ",
            "AUTO_MERGE_ATTACHMENT, REMOVE_HANDLER, APPLY_SHARED_EDITING ",
            "FROM AITK_WORK_ST_INFO ",
            "WHERE ID=#{id,jdbcType=VARCHAR}"
    })
    @ResultMap(value = {"WorkSubTaskInfoResultMap"})
    WorkSubTaskInfo getWorkSubTaskInfoById(final String id);

    /**
     * 插入审批记录信息.
     *
     * @param info 审批记录信息
     */
    @Insert({
            "insert into AITK_WORK_APPROVE_OPINION (ID, APPROVE_TIME, ",
            "APPROVE_UID, APPROVE_NAME, OPINION, DECISION, DEPT_NAME, DEPT_DN,",
            "WORK_APPLY_ID, NODE_ID, OPERATION_DESCRIPTION ,SUB_TASK_ID,WORK_STATUS, COPY_SUBMIT_UID, COPY_SUBMIT_NAME) ",
            "values ",
            "(#{id,jdbcType=VARCHAR}, #{approveTime,jdbcType=TIMESTAMP}, ",
            "#{approveUid,jdbcType=VARCHAR}, #{approveName,jdbcType=VARCHAR}, ",
            "#{opinion,jdbcType=VARCHAR}, #{decision,jdbcType=VARCHAR}, ",
            "#{deptName,jdbcType=VARCHAR}, #{deptDn,jdbcType=VARCHAR}, ",
            "#{workApplyId,jdbcType=VARCHAR}, #{nodeId,jdbcType=VARCHAR}, ",
            "#{operationDescription,jdbcType=VARCHAR},#{subTaskId,jdbcType=VARCHAR},#{workStatus,jdbcType=VARCHAR}, ",
            "#{copySubmitUid,jdbcType=CLOB}, #{copySubmitName,jdbcType=CLOB})"
    })
    void insertWorkApproveOpinion(final WorkApproveOpinion info);

    /**
     * 通过id查询审批意见信息集合.
     *
     * @param workApplyId 主工单id
     * @return 审批意见信息集合
     */
    @Select({
            "select ID, APPROVE_TIME, ",
            "APPROVE_UID, APPROVE_NAME, OPINION, DECISION, DEPT_NAME, DEPT_DN, ",
            "WORK_APPLY_ID,NODE_ID, OPERATION_DESCRIPTION, COPY_SUBMIT_UID, COPY_SUBMIT_NAME ",
            "FROM AITK_WORK_APPROVE_OPINION ",
            "WHERE WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} ",
            "ORDER BY APPROVE_TIME DESC "
    })
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "APPROVE_TIME", property = "approveTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "APPROVE_UID", property = "approveUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "APPROVE_NAME", property = "approveName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "OPINION", property = "opinion", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DECISION", property = "decision", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DEPT_NAME", property = "deptName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "DEPT_DN", property = "deptDn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "NODE_ID", property = "nodeId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "OPERATION_DESCRIPTION", property = "operationDescription", jdbcType = JdbcType.VARCHAR),
            @Result(column = "COPY_SUBMIT_UID", property = "copySubmitUid", jdbcType = JdbcType.CLOB),
            @Result(column = "COPY_SUBMIT_NAME", property = "copySubmitName", jdbcType = JdbcType.CLOB)
    })
    List<WorkApproveOpinion> findWorkApproveOpinionsByApplyId(final String workApplyId);

    /**
     * 插入子任务的附件信息.
     *
     * @param info 子任务的处理信息
     */
    @Insert({
            "insert into AITK_WORK_ST_ATTACHMENT (ID, UPLOAD_TIME, ",
            "FILE_NAME, ONLY_READ, UPLOADER_UID, UPLOADER_NAME, ATTACHMENT, ",
            "WORK_APPLY_ID, ATTACHMENT_TYPE, SUB_TASK_ID, WPS_FILE_ID) ",
            "values ",
            "(#{id,jdbcType=VARCHAR}, #{uploadTime,jdbcType=TIMESTAMP}, ",
            "#{fileName,jdbcType=VARCHAR}, #{onlyRead,jdbcType=BOOLEAN}, ",
            "#{uploaderUid,jdbcType=VARCHAR}, #{uploaderName,jdbcType=VARCHAR}, ",
            "#{attachment,jdbcType=BINARY}, #{workApplyId,jdbcType=VARCHAR}, ",
            "#{type,jdbcType=VARCHAR}, #{subTaskId,jdbcType=VARCHAR}, ",
            "#{wpsFileId,jdbcType=VARCHAR})"
    })
    void insertWorkApplyInfoAttachment(WorkApplyInfoAttachment info);

    /**
     * 插入子任务的附件信息.
     *
     * @param info 子任务的处理信息
     */
    @Update({
            "update AITK_WORK_ST_ATTACHMENT set UPLOAD_TIME=#{uploadTime,jdbcType=TIMESTAMP}, ",
            "FILE_NAME=#{fileName,jdbcType=VARCHAR}, ONLY_READ=#{onlyRead,jdbcType=BOOLEAN}, ",
            "UPLOADER_UID=#{uploaderUid,jdbcType=VARCHAR}, UPLOADER_NAME=#{uploaderName,jdbcType=VARCHAR}, ",
            "ATTACHMENT=#{attachment,jdbcType=BINARY}, ",
            "WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR}, WPS_FILE_ID=#{wpsFileId,jdbcType=VARCHAR} ",
            "where ID=#{id,jdbcType=VARCHAR}"
    })
    void updateWorkApplyInfoAttachment(WorkApplyInfoAttachment info);

    /**
     * 更新附件的wps文件id.
     * @param id 表id
     * @param wpsFileId 文件id
     */
    @Update({
            "update AITK_WORK_ST_ATTACHMENT set WPS_FILE_ID=#{wpsFileId,jdbcType=VARCHAR} where ID=#{id,jdbcType=VARCHAR}"
    })
    void updateAttachmentWpsFileId(@Param("id") final String id, @Param("wpsFileId")  final String wpsFileId);

    /**
     * 更新附件信息.
     * @param info 附件
     */
    @Update({
            "update AITK_WORK_ST_ATTACHMENT set ATTACHMENT=#{attachment,jdbcType=BINARY} where WPS_FILE_ID=#{wpsFileId,jdbcType=VARCHAR}"
    })
    void updateAttachmentByWpsFileId(final WorkApplyInfoAttachment info);

    /**
     * 通过分任务处理信息id查询附件信息集合.
     *
     * @param workApplyId 工单信息id
     * @return 分任务处理附件信息集合
     */
    @Select({
            "select ID, UPLOAD_TIME, ",
            "FILE_NAME, ONLY_READ, UPLOADER_UID, UPLOADER_NAME, ATTACHMENT,",
            "WORK_APPLY_ID, ATTACHMENT_TYPE, WPS_FILE_ID ",
            "FROM AITK_WORK_ST_ATTACHMENT ",
            "WHERE WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} and ATTACHMENT_TYPE=#{type,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "UPLOAD_TIME", property = "uploadTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "FILE_NAME", property = "fileName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ONLY_READ", property = "onlyRead", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "UPLOADER_UID", property = "uploaderUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "UPLOADER_NAME", property = "uploaderName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ATTACHMENT", property = "attachment", jdbcType = JdbcType.BINARY),
            @Result(column = "ATTACHMENT_TYPE", property = "type", jdbcType = JdbcType.BINARY),
            @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "WPS_FILE_ID", property = "wpsFileId", jdbcType = JdbcType.VARCHAR)
    })
    List<WorkApplyInfoAttachment> findWorkApplyInfoAttachmentsByApplyId(@Param("workApplyId") String workApplyId,
                                                                        @Param("type") AttachTypeEnum type);

    /**
     * 通过分任务ids信息查询附件信息集合.
     *
     * @param workApplyIds 工单信息id
     * @return 分任务处理附件信息集合
     */
    @Select({
        "<script>",
        "select ID, UPLOAD_TIME, ",
        "FILE_NAME, ONLY_READ, UPLOADER_UID, UPLOADER_NAME, ATTACHMENT,",
        "WORK_APPLY_ID, ATTACHMENT_TYPE, WPS_FILE_ID ",
        "FROM AITK_WORK_ST_ATTACHMENT ",
        "WHERE WORK_APPLY_ID in ",
        "<foreach collection='workApplyIds' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
        "<if test=\"type != null \"> AND ATTACHMENT_TYPE=#{type,jdbcType=VARCHAR} </if> ",
        "</script>"
    })
    @Results({
        @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
        @Result(column = "UPLOAD_TIME", property = "uploadTime", jdbcType = JdbcType.TIMESTAMP),
        @Result(column = "FILE_NAME", property = "fileName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "ONLY_READ", property = "onlyRead", jdbcType = JdbcType.BOOLEAN),
        @Result(column = "UPLOADER_UID", property = "uploaderUid", jdbcType = JdbcType.VARCHAR),
        @Result(column = "UPLOADER_NAME", property = "uploaderName", jdbcType = JdbcType.VARCHAR),
        @Result(column = "ATTACHMENT", property = "attachment", jdbcType = JdbcType.BINARY),
        @Result(column = "ATTACHMENT_TYPE", property = "type", jdbcType = JdbcType.BINARY),
        @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
        @Result(column = "WPS_FILE_ID", property = "wpsFileId", jdbcType = JdbcType.VARCHAR)
    })
    List<WorkApplyInfoAttachment> findWorkApplyInfoAttachmentsByApplyIds(@Param("workApplyIds")  List<String> workApplyIds,
                                                                        @Param("type") AttachTypeEnum type);

    /**
     * 通过分任务id信息查询附件信息集合.
     *
     * @param subTaskId 分任务id
     * @return 分任务附件信息集合
     */
    @Select({
            "select ID, UPLOAD_TIME, ",
            "FILE_NAME, ONLY_READ, UPLOADER_UID, UPLOADER_NAME, ATTACHMENT,",
            "WORK_APPLY_ID, ATTACHMENT_TYPE,SUB_TASK_ID, WPS_FILE_ID ",
            "FROM AITK_WORK_ST_ATTACHMENT ",
            "WHERE SUB_TASK_ID=#{subTaskId,jdbcType=VARCHAR} and ATTACHMENT_TYPE=#{type,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "UPLOAD_TIME", property = "uploadTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "FILE_NAME", property = "fileName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ONLY_READ", property = "onlyRead", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "UPLOADER_UID", property = "uploaderUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "UPLOADER_NAME", property = "uploaderName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ATTACHMENT", property = "attachment", jdbcType = JdbcType.BINARY),
            @Result(column = "ATTACHMENT_TYPE", property = "type", jdbcType = JdbcType.BINARY),
            @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SUB_TASK_ID", property = "subTaskId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "WPS_FILE_ID", property = "wpsFileId", jdbcType = JdbcType.VARCHAR)

    })
    List<WorkApplyInfoAttachment> findWorkApplyInfoAttachmentsBySubTaskId(@Param("subTaskId") String subTaskId,
                                                                          @Param("type") AttachTypeEnum type);

    /**
     * 通过分任务id信息查询附件信息（不含附件内容）集合.
     *
     * @param subTaskIds 列表
     * @param type 类型
     * @return 分任务附件信息集合
     */
    @Select({
            "<script>",
            "select ID, UPLOAD_TIME, ",
            "FILE_NAME, ONLY_READ, UPLOADER_UID, UPLOADER_NAME, ",
            "WORK_APPLY_ID, ATTACHMENT_TYPE,SUB_TASK_ID, WPS_FILE_ID ",
            "FROM AITK_WORK_ST_ATTACHMENT ",
            "WHERE SUB_TASK_ID in ",
            "<foreach collection='subTaskIds' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
            "<if test=\"type != null \"> AND ATTACHMENT_TYPE=#{type,jdbcType=VARCHAR} </if> ",
            "</script>"
    })
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "UPLOAD_TIME", property = "uploadTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "FILE_NAME", property = "fileName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ONLY_READ", property = "onlyRead", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "UPLOADER_UID", property = "uploaderUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "UPLOADER_NAME", property = "uploaderName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ATTACHMENT_TYPE", property = "type", jdbcType = JdbcType.BINARY),
            @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SUB_TASK_ID", property = "subTaskId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "WPS_FILE_ID", property = "wpsFileId", jdbcType = JdbcType.VARCHAR)
    })
    List<WorkApplyInfoAttachment> findAttachmentExceptContentBySubTaskIds(@Param("subTaskIds") List<String> subTaskIds,
                                                                          @Param("type") AttachTypeEnum type);

    @Select({
            "<script>",
            "select ID, UPLOAD_TIME, ",
            "FILE_NAME, ONLY_READ, UPLOADER_UID, UPLOADER_NAME, ",
            "WORK_APPLY_ID, ATTACHMENT_TYPE,SUB_TASK_ID, WPS_FILE_ID ",
            "FROM AITK_WORK_ST_ATTACHMENT ",
            "WHERE WORK_APPLY_ID in ",
            "<foreach collection='workApplyIds' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
            "<if test=\"type != null \"> AND ATTACHMENT_TYPE=#{type,jdbcType=VARCHAR} </if> ",
            "</script>"
    })
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "UPLOAD_TIME", property = "uploadTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "FILE_NAME", property = "fileName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ONLY_READ", property = "onlyRead", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "UPLOADER_UID", property = "uploaderUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "UPLOADER_NAME", property = "uploaderName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ATTACHMENT_TYPE", property = "type", jdbcType = JdbcType.BINARY),
            @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "SUB_TASK_ID", property = "subTaskId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "WPS_FILE_ID", property = "wpsFileId", jdbcType = JdbcType.VARCHAR)
    })
    List<WorkApplyInfoAttachment> findAttachmentExceptContentByWorkApplyIds(@Param("workApplyIds") Set<String> workApplyIds,
                                                                            @Param("type") AttachTypeEnum type);
    /**
     * 更新主工单信息.
     *
     * @param workApplyInfo 工单信息
     */
    @Update({
            "update AITK_WORK_APPLY_INFO set PIID=#{piid,jdbcType=VARCHAR}, ",
            "APPLY_ID=#{applyId,jdbcType=VARCHAR}, TITLE=#{title,jdbcType=VARCHAR}, PARENT_ID=#{parentId,jdbcType=VARCHAR}, ",
            "STATUS=#{status,jdbcType=VARCHAR}, HANDLER_UID=#{handlerUid,jdbcType=VARCHAR}, HANDLER_NAME=#{handlerName,jdbcType=VARCHAR}, ",
            "RECEIVER_UID=#{receiverUid,jdbcType=VARCHAR}, RECEIVER_NAME=#{receiverName,jdbcType=VARCHAR}, ",
            "CREATOR_MOBILE=#{creatorMobile,jdbcType=VARCHAR}, CREATOR_DEPT_DN=#{creatorDeptDn,jdbcType=VARCHAR}, ",
            "CREATOR_DEPT_NAME=#{creatorDeptName,jdbcType=VARCHAR}, CREATOR_UID=#{creatorUid,jdbcType=VARCHAR}, ",
            "CREATOR_NAME=#{creatorName,jdbcType=VARCHAR}, MAIN_TASK_NAME=#{mainTaskName,jdbcType=VARCHAR}, ",
            "ASSIGN_USER_ID=#{assignUserId,jdbcType=VARCHAR}, ASSIGN_USER_NAME=#{assignUserName,jdbcType=VARCHAR},",
            "DISPATCHED=#{dispatched,jdbcType=VARCHAR}, ",
            "UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP}, HANDLE_RESULT=#{handleResult,jdbcType=VARCHAR}, ",
            "FLOW_TYPE=#{flowType,jdbcType=VARCHAR}, CREATOR_PARTY_ORG_NAME=#{creatorPartyOrgName,jdbcType=VARCHAR}, ",
            "CHAPTER_DATE=#{chapterDate,jdbcType=TIMESTAMP}, CHAPTER_DOC=#{chapterDoc,jdbcType=VARCHAR}, ",
            "CHAPTER_DOC_OTHER=#{chapterDocOther,jdbcType=VARCHAR}, DOC_NUM=#{docNum,jdbcType=INTEGER}, ",
            "SEND_UNIT=#{sendUnit,jdbcType=VARCHAR}, CONTENT_SUMMARY=#{contentSummary,jdbcType=VARCHAR}, ",
            "PARTY_LEADER_UID=#{partyLeaderUid,jdbcType=VARCHAR},PARTY_LEADER_NAME=#{partyLeaderName,jdbcType=VARCHAR},",
            "MAIN_WORK_APPLY_ID=#{mainWorkApplyId,jdbcType=VARCHAR},NOTIFY_AFTER_REPORT_ALL=#{notifyAfterReportAll,jdbcType=BOOLEAN}, ",
            "USESEALTYPE=#{useSealType,jdbcType=VARCHAR}, PROJECT_CATEGORY = #{projectCategory,jdbcType=VARCHAR}, ",
            "KEY_AREAS_TO_TACKLE = #{keyAreasToTackle,jdbcType=VARCHAR}, DESCRIPTION = #{description,jdbcType=VARCHAR}, ",
            "CONTACTS_UID = #{contactsUid,jdbcType=VARCHAR}, CONTACTS_NAME = #{contactsName,jdbcType=VARCHAR}, ",
            "CONTACTS_MOBILE = #{contactsMobile,jdbcType=VARCHAR}, PARTICIPANTS_UID = #{participantsUid,jdbcType=VARCHAR}, ",
            "PARTICIPANTS_NAME = #{participantsName,jdbcType=VARCHAR}, RECEIVER_LABEL = #{receiverLabel,jdbcType=VARCHAR}, ",
            "PARTICIPANTS_LABEL = #{participantsLabel,jdbcType=VARCHAR}, CONTACTS_LABEL = #{contactsLabel,jdbcType=VARCHAR} ",
            "where ID=#{id,jdbcType=VARCHAR}",
    })
    void updateWorkApplyInfo(final WorkApplyInfo workApplyInfo);

    /**
     * 更新分任务.
     *
     * @param info 分任务信息
     */
    @Update({
            "update AITK_WORK_ST_INFO set NAME=#{name,jdbcType=VARCHAR}, ",
            "DESCRIPTION=#{description,jdbcType=VARCHAR}, DEPT_DNS=#{deptDns,jdbcType=VARCHAR}, ",
            "DEPT_NAMES=#{deptNames,jdbcType=VARCHAR}, ",
            "FINISH_TIME=#{finishTime,jdbcType=TIMESTAMP}, WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR}, ",
            "PARENT_WORK_APPLY_ID=#{parentWorkApplyId,jdbcType=VARCHAR}, PARENT_TASK_ID=#{parentTaskId,jdbcType=VARCHAR}, ",
            "NODE=#{node,jdbcType=VARCHAR}, STATE=#{state,jdbcType=VARCHAR}, UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP}, ",
            "ORDER_INDEX=#{orderIndex, jdbcType=INTEGER}, MAIN_TASK_ID=#{mainTaskId,jdbcType=VARCHAR}, ",
            "REPORT_MERGE_ATTACHMENT=#{reportMergeAttachment,jdbcType=BOOLEAN}, ",
            "REPORT_COUNT=#{reportCount,jdbcType=INTEGER},AUTO_MERGE_ATTACHMENT=#{autoMergeAttachment,jdbcType=BOOLEAN}, ",
            "REMOVE_HANDLER=#{removeHandler,jdbcType=BOOLEAN}, APPLY_SHARED_EDITING=#{applySharedEditing,jdbcType=BOOLEAN}",
            "where ID=#{id,jdbcType=VARCHAR} "
    })
    void updateWorkSubTask(WorkSubTaskInfo info);

    /**
     * 删除工单任务.
     *
     * @param id 工单id
     */
    @Delete({
            "delete from AITK_WORK_APPLY_INFO where ID=#{id,jdbcType=VARCHAR}"
    })
    void deleteWorkApplyById(String id);

    /**
     * 删除分任务.
     *
     * @param id 分任务id
     */
    @Delete({
            "delete from AITK_WORK_ST_INFO where ID=#{id,jdbcType=VARCHAR}"
    })
    void deleteSubTaskById(String id);

    /**
     * 删除分任务.
     *
     * @param idList 分任务id列表
     */
    @Delete({
            "<script>",
            "delete from AITK_WORK_ST_INFO where ID IN ",
            "<foreach collection='idList' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
            "</script>"
    })
    void deleteSubTaskByIdList(@Param("idList") List<String> idList);

    /**
     * 删除附件.
     *
     * @param id 附件id
     */
    @Delete({
            "delete from AITK_WORK_ST_ATTACHMENT where ID=#{id,jdbcType=VARCHAR}"
    })
    void deleteAttachmentById(String id);

    /**
     * 通过id查询工单附件.
     *
     * @param attachmentId 工单附件主键id
     * @return 附件对象
     */
    @Select({
            "select ID, UPLOAD_TIME, ",
            "FILE_NAME, ONLY_READ, UPLOADER_UID, UPLOADER_NAME, ATTACHMENT, ",
            "WORK_APPLY_ID, WPS_FILE_ID ",
            "FROM AITK_WORK_ST_ATTACHMENT ",
            "WHERE ID=#{attachmentId,jdbcType=VARCHAR} "
    })
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "UPLOAD_TIME", property = "uploadTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "FILE_NAME", property = "fileName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ONLY_READ", property = "onlyRead", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "UPLOADER_UID", property = "uploaderUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "UPLOADER_NAME", property = "uploaderName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ATTACHMENT", property = "attachment", jdbcType = JdbcType.BINARY),
            @Result(column = "WORK_APPLY_ID", property = "workApplyId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "WPS_FILE_ID", property = "wpsFileId", jdbcType = JdbcType.VARCHAR)
    })
    WorkApplyInfoAttachment findWorkApplyInfoAttachmentsById(String attachmentId);

    /**
     * 工单查询.
     *
     * @param vo 查询参数
     * @return 查询结果
     */
    @Select({
            "<script>",
            "select ID, PIID, ",
            "APPLY_ID, TITLE, PARENT_ID,t.current_status as status, ",
            "t.current_handler_uid as HANDLER_UID, t.current_handler_name as HANDLER_NAME, HANDLE_RESULT, ",
            "RECEIVER_UID, RECEIVER_NAME, ",
            "CREATOR_MOBILE, CREATOR_DEPT_DN, ",
            "CREATOR_DEPT_NAME, CREATOR_UID, ",
            "CREATOR_NAME, MAIN_TASK_NAME, CREATE_TIME, UPDATE_TIME, ",
            "FLOW_TYPE,CREATOR_PARTY_ORG_NAME,CHAPTER_DATE,CHAPTER_DOC,",
            "CHAPTER_DOC_OTHER,DOC_NUM,SEND_UNIT,CONTENT_SUMMARY,PARTY_LEADER_UID,PARTY_LEADER_NAME,",
            "MAIN_WORK_APPLY_ID,NOTIFY_AFTER_REPORT_ALL ",
            "FROM aitk_work_apply_view t WHERE id = MAIN_WORK_APPLY_ID ",
            "<if test=\"status != null \"> AND STATUS=#{status} </if> ",
            "<if test=\"creatorUid != null and creatorUid != ''\"> AND CREATOR_UID=#{creatorUid,jdbcType=VARCHAR} </if> ",
            "<if test=\"userId != null and userId != ''\"> AND HANDLER_UID=#{userId,jdbcType=VARCHAR} </if> ",
            "<if test=\"applyTitle != null and applyTitle != ''\"> ",
            "AND title like CONCAT('%', CONCAT(#{applyTitle,jdbcType=VARCHAR}, '%')) ",
            "</if> ",
            "<if test=\"excludeDraft\"> AND (STATUS != 'DRAFT' ",
            " or  exists (select 1 from  aitk_work_approve_opinion where work_apply_id = t.id)",
            ")</if> ",
            "<if test=\"orderField != null and orderField != ''\"> order by ${orderField}  ${orderType} </if> ",
            "</script>"
    })
    @ResultMap(value = {"WorkApplyInfoResultMap"})
    List<WorkApplyInfo> search(WorkApplyQueryVO vo);

    /**
     * 根据工单id和类型删除附件.
     *
     * @param workApplyId 附件id
     * @param type        类型
     */
    @Delete({
            "delete from AITK_WORK_ST_ATTACHMENT where WORK_APPLY_ID=#{workApplyId,jdbcType=VARCHAR} and ATTACHMENT_TYPE=#{type,jdbcType=VARCHAR} "
    })
    void deleteAttachmentByWorkApplyId(@Param("workApplyId") String workApplyId,
                                       @Param("type") AttachTypeEnum type);

    /**
     * 根据分任务id删除附件.
     *
     * @param subTaskId 分任务id
     */
    @Delete({
            "delete from AITK_WORK_ST_ATTACHMENT where SUB_TASK_ID=#{subTaskId,jdbcType=VARCHAR} "
    })
    void deleteAttachmentBySubTaskId(@Param("subTaskId") String subTaskId);

    /**
     * 根据分任务id列表删除附件.
     *
     * @param subTaskIdList 分任务id列表
     */
    @Delete({
            "<script>",
            "delete from AITK_WORK_ST_ATTACHMENT where SUB_TASK_ID IN ",
            "<foreach collection='subTaskIdList' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
            "</script>"
    })
    void deleteAttachmentBySubTaskIdList(@Param("subTaskIdList") List<String> subTaskIdList);

    @Select({"select a.id,a.HANDLER_UID,a.HANDLER_NAME,a.status ,a.title,a.create_time,a.apply_Id " +
            "from aitk_work_apply_info a left join aitk_work_st_info b on a.id=b.work_apply_id " +
            "where a.status in ('RESPONDER_APPROVE','SUB_ORG_USER_HANDLE','SUB_ORG_USER_CONFIRM') " +
            "and b.finish_time > #{start,jdbcType=TIMESTAMP} and b.finish_time < #{end,jdbcType=TIMESTAMP}"})
    @Results({
            @Result(column = "ID", property = "id", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "STATUS", property = "status", jdbcType = JdbcType.VARCHAR),
            @Result(column = "HANDLER_UID", property = "handlerUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "HANDLER_NAME", property = "handlerName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.VARCHAR),
            @Result(column = "apply_Id", property = "applyId", jdbcType = JdbcType.VARCHAR),
    })
    List<WorkApplyInfo> getWarningUserByTime(@Param("start") Date start, @Param("end") Date end);

    /**
     * 查询指定用户下所有待办子任务的要求完成时间.
     *
     * @param handlerId 用户id
     * @param finishTime 完成时间
     * @return 该用户名下智办工单集合
     */
    @Select({
            "SELECT st.ID, st.NAME,",
            "st.DESCRIPTION, st.DEPT_DNS, ",
            "st.DEPT_NAMES, st.FINISH_TIME, st.WORK_APPLY_ID, ",
            "st.PARENT_WORK_APPLY_ID, st.PARENT_TASK_ID, st.NODE, st.STATE, st.UPDATE_TIME, st.ORDER_INDEX, st.MAIN_TASK_ID, ",
            "st.REPORT_MERGE_ATTACHMENT, st.REPORT_COUNT, st.AUTO_MERGE_ATTACHMENT, st.REMOVE_HANDLER, st.APPLY_SHARED_EDITING ",
            "FROM AITK_WORK_APPLY_INFO app LEFT JOIN AITK_WORK_ST_INFO st ",
            "ON app.ID = st.WORK_APPLY_ID ",
            "WHERE app.STATUS IN ('RESPONDER_APPROVE','SUB_ORG_USER_HANDLE','SUB_ORG_USER_CONFIRM') ",
            "And app.HANDLER_UID = #{handlerId,jdbcType=VARCHAR}",
            "And st.FINISH_TIME >= #{finishTIme,jdbcType=TIMESTAMP}",
    })
    @ResultMap(value = {"WorkSubTaskInfoResultMap"})
    List<WorkSubTaskInfo> findWorkSubTasksByUserId(@Param("handlerId") String handlerId, @Param("finishTIme") Date finishTime);

    /**
     * 查询获取指定父任务ID的子任务列表.
     *
     * @param parentTaskId 父任务ID
     * @return 子任务集合
     */
    @Select({
            "select ID, NAME, ",
            "DESCRIPTION, DEPT_DNS, ",
            "DEPT_NAMES, FINISH_TIME, ",
            "WORK_APPLY_ID, PARENT_WORK_APPLY_ID, ",
            "PARENT_TASK_ID, NODE, STATE, UPDATE_TIME, ORDER_INDEX, MAIN_TASK_ID, ",
            "REPORT_MERGE_ATTACHMENT, REPORT_COUNT, AUTO_MERGE_ATTACHMENT, ",
            "REMOVE_HANDLER, APPLY_SHARED_EDITING ",
            "FROM AITK_WORK_ST_INFO ",
            "WHERE PARENT_TASK_ID=#{parentTaskId,jdbcType=VARCHAR}"
    })
    @ResultMap(value = {"WorkSubTaskInfoResultMap"})
    List<WorkSubTaskInfo> getWorkSubTaskInfoByParentTaskId(final String parentTaskId);

    @Select({
            "<script>",
            "select ID, NAME, ",
            "DESCRIPTION, DEPT_DNS, ",
            "DEPT_NAMES, FINISH_TIME, ",
            "WORK_APPLY_ID, PARENT_WORK_APPLY_ID, ",
            "PARENT_TASK_ID, NODE, STATE, UPDATE_TIME, ORDER_INDEX, MAIN_TASK_ID, ",
            "REPORT_MERGE_ATTACHMENT, REPORT_COUNT, AUTO_MERGE_ATTACHMENT, ",
            "REMOVE_HANDLER, APPLY_SHARED_EDITING ",
            "FROM AITK_WORK_ST_INFO WHERE ID IN ",
            "<foreach collection='idList' item='item' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    @ResultMap(value = {"WorkSubTaskInfoResultMap"})
    List<WorkSubTaskInfo> getWorkSubTaskInfoByIds(@Param("idList") final List<String> idList);

    /**
    * 查询我的已办.
    * @param vo 查询参数
    * @return 列表.
    */
    @Select({
        "<script>",
        "select t.ID, t.PIID, ",
        "t.APPLY_ID, t.TITLE, t.PARENT_ID, t.current_status as status, ",
        "t.current_handler_uid as HANDLER_UID, t.current_handler_name as HANDLER_NAME, t.HANDLE_RESULT, ",
        "t.RECEIVER_UID, t.RECEIVER_NAME, ",
        "t.CREATOR_MOBILE, t.CREATOR_DEPT_DN, ",
        "t.CREATOR_DEPT_NAME, t.CREATOR_UID, ",
        "t.CREATOR_NAME, t.MAIN_TASK_NAME, t.CREATE_TIME, t.UPDATE_TIME, ",
        "t.FLOW_TYPE, t.CREATOR_PARTY_ORG_NAME, t.CHAPTER_DATE,t.CHAPTER_DOC,",
        "t.CHAPTER_DOC_OTHER, t.DOC_NUM,t.SEND_UNIT, t.CONTENT_SUMMARY, t.PARTY_LEADER_UID,",
         " t.PARTY_LEADER_NAME,t.NOTIFY_AFTER_REPORT_ALL, t.MAIN_WORK_APPLY_ID",
        "FROM aitk_work_done_info o left join aitk_work_apply_view t ON o.work_apply_id = t.id WHERE 1=1 ",
        "<if test=\"approveUid != null and approveUid != ''\"> AND o.owner_uid=#{approveUid,jdbcType=VARCHAR} </if> ",
        "<if test=\"applyTitle != null and applyTitle != ''\"> ",
        "AND t.title like CONCAT('%', CONCAT(#{applyTitle,jdbcType=VARCHAR}, '%')) ",
        "</if> ",
        "<if test=\"orderField != null and orderField != ''\"> order by ${orderField}  ${orderType} </if> ",
        "</script>"
    })
    @ResultMap(value = {"WorkApplyInfoResultMap"})
    List<WorkApplyInfo> queryDoneTask(WorkApplyQueryVO vo);

    /**
     * 根据工单id获取工单.
     * 子方法只能用于我的工作台场景.
     * @param applyIds 工单id
     * @return
     */
    @Select({
        "<script>",
        "select t.ID, t.PIID, ",
        "t.APPLY_ID, t.TITLE, t.PARENT_ID, t.current_status as status, ",
        "t.current_handler_uid as HANDLER_UID, t.current_handler_name as HANDLER_NAME, t.HANDLE_RESULT, ",
        "t.RECEIVER_UID, t.RECEIVER_NAME, ",
        "t.CREATOR_MOBILE, t.CREATOR_DEPT_DN, ",
        "t.CREATOR_DEPT_NAME, t.CREATOR_UID, ",
        "t.CREATOR_NAME, t.MAIN_TASK_NAME, t.CREATE_TIME, t.UPDATE_TIME, ",
        "t.FLOW_TYPE, t.CREATOR_PARTY_ORG_NAME, t.CHAPTER_DATE,t.CHAPTER_DOC,",
        "t.CHAPTER_DOC_OTHER, t.DOC_NUM,t.SEND_UNIT, t.CONTENT_SUMMARY,",
        " t.PARTY_LEADER_UID, t.PARTY_LEADER_NAME, t.NOTIFY_AFTER_REPORT_ALL, t.MAIN_WORK_APPLY_ID",
        "FROM aitk_work_apply_view t  WHERE APPLY_ID in  ",
        "<foreach collection='applyIds' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
        "</script>"
    })
    @ResultMap(value = {"WorkApplyInfoResultMap"})
    List<WorkApplyInfo> workApplyInfoByApplyIdsForWorkCenterShow(@Param("applyIds") Set<String> applyIds);


    /**
     * 根据父工单id查询子工单并排除指定id.
     * @param parentApplyId 父工单id
     * @param ids 需要排除的id
     * @return 工单列表
     */
    @Select({
        "<script>",
        "select ID, PIID, ",
        "APPLY_ID, TITLE, ASSIGN_USER_ID, ASSIGN_USER_NAME, STATUS, ",
        "HANDLER_UID, HANDLER_NAME, HANDLE_RESULT, ",
        "RECEIVER_UID, RECEIVER_NAME, ",
        "CREATOR_MOBILE, CREATOR_DEPT_DN, DISPATCHED, ",
        "CREATOR_DEPT_NAME, CREATOR_UID, PARENT_ID, ",
        "CREATOR_NAME, MAIN_TASK_NAME, CREATE_TIME, UPDATE_TIME, ",
        "FLOW_TYPE,CREATOR_PARTY_ORG_NAME,CHAPTER_DATE,CHAPTER_DOC,",
        "CHAPTER_DOC_OTHER,DOC_NUM,SEND_UNIT,CONTENT_SUMMARY,PARTY_LEADER_UID,",
        "PARTY_LEADER_NAME, MAIN_WORK_APPLY_ID,NOTIFY_AFTER_REPORT_ALL ",
        "FROM AITK_WORK_APPLY_INFO ",
        "WHERE ID NOT IN ",
        "<foreach collection='ids' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
        "and PARENT_ID = #{parentApplyId,jdbcType=VARCHAR}",
        "</script>"
    })
    @ResultMap(value = {"WorkApplyInfoResultMap"})
    List<WorkApplyInfo> findWorkApplyInfoByParentIdExcludeIds(@Param("parentApplyId") String parentApplyId, @Param("ids")Set<String> ids);

    /**
     * 更新工单通知方式.
     *
     * @param id 工单id，
     * @param notifyAfterReportAll 是否上报全部后通知
     */
    @Update({
            "update AITK_WORK_APPLY_INFO set NOTIFY_AFTER_REPORT_ALL = #{notifyAfterReportAll,jdbcType=BOOLEAN} ",
            "where ID=#{id,jdbcType=VARCHAR} "
    })
    void updateNotifyMethod(@Param("id") String id, @Param("notifyAfterReportAll") boolean notifyAfterReportAll);

    /**
     * 查询获取指定父任务ID的子任务列表.
     *
     * @param parentApplyId 父工单id
     * @param ids 已经加载到内存的id
     * @return 子任务集合
     */
    @Select({
            "<script>",
            "select ID, NAME, ",
            "DESCRIPTION, DEPT_DNS, ",
            "DEPT_NAMES, FINISH_TIME, ",
            "WORK_APPLY_ID, PARENT_WORK_APPLY_ID, ",
            "PARENT_TASK_ID, NODE, STATE, UPDATE_TIME, ORDER_INDEX, MAIN_TASK_ID,",
            "REPORT_MERGE_ATTACHMENT, REPORT_COUNT, AUTO_MERGE_ATTACHMENT, ",
            "REMOVE_HANDLER, APPLY_SHARED_EDITING ",
            "FROM AITK_WORK_ST_INFO ",
            "WHERE PARENT_WORK_APPLY_ID=#{parentApplyId,jdbcType=VARCHAR}",
            "<if test=\"ids != null and ids.size() > 0  \"> ",
            " AND ID NOT IN ",
            "<foreach collection='ids' item='item' open='(' separator=',' close=')'> #{item} </foreach>",
            "</if> ",
            "</script>"
    })
    @ResultMap(value = {"WorkSubTaskInfoResultMap"})
    List<WorkSubTaskInfo> getWorkSubTaskInfoByParentTaskIdExcludeIds(@Param("parentApplyId") String parentApplyId, @Param("ids")Set<String> ids);

    /**
     * 更新工单状态.
     *
     * @param id 工单id
     * @param updateTime 当前时间
     */
    @Update({
            "begin",
            "update aitk_work_apply_info set UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP}, ",
            "HANDLER_UID=NULL, HANDLER_NAME=NULL,STATUS='DISPLAY_TERMINATE'",
            "where ID=#{id,jdbcType=VARCHAR};",
            "update aitk_work_status_info set UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP}, ",
            "CURRENT_HANDLER_UID=NULL, CURRENT_HANDLER_NAME=NULL,CURRENT_STATUS='DISPLAY_TERMINATE'",
            "where WORK_APPLY_ID=#{id,jdbcType=VARCHAR};",
            "update aitk_work_st_info set UPDATE_TIME=#{updateTime,jdbcType=TIMESTAMP},",
            "node = 'TERMINATE', state = 'SUSPEND' ",
            "where  WORK_APPLY_ID=#{id,jdbcType=VARCHAR} and node != 'FINISH';",
            "end;"
    })
    void updateApplyStatusToTerminate(@Param("id") String id, @Param("updateTime") Date updateTime);

    @Delete({
            "begin",
            "delete from aitk_work_apply_info where ID = #{id,jdbcType=VARCHAR}; ",
            "delete from aitk_work_status_info where WORK_APPLY_ID = #{id,jdbcType=VARCHAR}; ",
            "delete from aitk_work_done_info where WORK_APPLY_ID = #{id,jdbcType=VARCHAR}; ",
            "delete from aitk_work_st_info where WORK_APPLY_ID = #{id,jdbcType=VARCHAR}; ",
            "delete from aitk_work_st_attachment where WORK_APPLY_ID = #{id,jdbcType=VARCHAR}; ",
            "end;"
    })
    void deleteDraft(@Param("id") String id);

    @Insert({
            "<script>",
            "<foreach collection='workCopySubmitInfos' open='begin' item='item' separator=';' close=';end;'>",
            "insert into AITK_WORK_COPY_SUBMIT_INFO (ID, WORK_APPLY_ID, COPY_SUBMIT_UID, COPY_SUBMIT_NAME, READ_TYPE, CREATE_TIME, UPDATE_TIME)",
            "values ",
            "(#{item.id, jdbcType=VARCHAR}, #{item.workApplyId, jdbcType=VARCHAR}, #{item.copySubmitUid, jdbcType=CLOB}, ",
            "#{item.copySubmitName, jdbcType=CLOB}, #{item.readType, jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP})",
            "</foreach>",
            "</script>"
    })
    void insertCopySubmitInfo(@Param("workCopySubmitInfos") List<WorkCopySubmitInfo> workCopySubmitInfos);

    /**
     * 查询我的阅办列表.
     * @param vo 查询参数
     * @return 列表.
     */
    @Select({
            "<script>",
            "select t.ID, t.PIID, ",
            "t.APPLY_ID, t.TITLE, t.PARENT_ID, t.current_status as status, ",
            "t.current_handler_uid as HANDLER_UID, t.current_handler_name as HANDLER_NAME, t.HANDLE_RESULT, ",
            "t.RECEIVER_UID, t.RECEIVER_NAME, ",
            "t.CREATOR_MOBILE, t.CREATOR_DEPT_DN, ",
            "t.CREATOR_DEPT_NAME, t.CREATOR_UID, ",
            "t.CREATOR_NAME, t.MAIN_TASK_NAME, t.CREATE_TIME, t.UPDATE_TIME, ",
            "t.FLOW_TYPE, t.CREATOR_PARTY_ORG_NAME, t.CHAPTER_DATE,t.CHAPTER_DOC,",
            "t.CHAPTER_DOC_OTHER, t.DOC_NUM,t.SEND_UNIT, t.CONTENT_SUMMARY, t.PARTY_LEADER_UID,",
            " t.PARTY_LEADER_NAME,t.NOTIFY_AFTER_REPORT_ALL, t.MAIN_WORK_APPLY_ID",
            "FROM AITK_WORK_COPY_SUBMIT_INFO o left join aitk_work_apply_view t ON o.work_apply_id = t.id WHERE 1=1 ",
            "<if test=\"userId != null and userId != ''\"> AND o.COPY_SUBMIT_UID=#{userId,jdbcType=VARCHAR} </if> ",
            "<if test=\"workType != null and workType != ''\"> ",
            "AND o.READ_TYPE=#{workType,jdbcType=VARCHAR} ",
            "</if> ",
            "order by CREATE_TIME desc",
            "</script>"
    })
    @ResultMap(value = {"WorkApplyInfoResultMap"})
    List<WorkApplyInfo> queryReadTask(WorkApplyQueryVO vo);

    @Select({
            "select READ_TYPE from AITK_WORK_COPY_SUBMIT_INFO ",
             "where work_apply_id = #{workApplyId, jdbcType=VARCHAR} and COPY_SUBMIT_UID = #{userId, jdbcType=VARCHAR} "
    })
    String getReadTypeByWorkApplyId(@Param("workApplyId") String workApplyId, @Param("userId") String userId);

    @Update({
            "update AITK_WORK_COPY_SUBMIT_INFO set READ_TYPE = 'hasRead', UPDATE_TIME = SYSDATE",
            "where work_apply_id = #{workApplyId, jdbcType=VARCHAR} and COPY_SUBMIT_UID = #{userId, jdbcType=VARCHAR} "
    })
    void updateReadType(@Param("workApplyId") String workApplyId, @Param("userId") String userId);
}
